/* ***** BEGIN LICENSE BLOCK *****
 *   Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Seoprofesional
 *
 * The Initial Developer of the Original Code is
 * Petr Blahoš.
 * Portions created by the Initial Developer are Copyright (C) 2009
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * 
 * ***** END LICENSE BLOCK ***** */

// Note: this is not an overlay, therefore we do not need to worry about 
// polluting the global namespace...

var links_view = {
dataArray: [ ],
init: function() {
	this.dataElement = document.getElementById("menulinks");
	try {
		this.dataArray = JSON.parse(this.dataElement.value);
	} catch (e) {
		SeoprofesionalTools.LOG("Error loading menulinks: " + e);
		this.dataArray = links_view.seop_global.menulinks_default;
	}
	SeoprofesionalTools.LOG("MENULINKS:" + this.dataArray);
},

treeBox: null,  

get rowCount()                     { return this.dataArray.length; },
setTree: function(treeBox)         { this.treeBox = treeBox; },
getCellValue: function(idx, column) {
	if ("active"==column.id) {
		return this.dataArray[idx][0];
	}
	return this.getCellText(idx, column);
},
setCellValue: function(idx,column,t) {
	this.dataArray[idx][0] = (t == "true" ? true : false);
	this.treeBox.invalidateRow(idx);
	this.dataElement.value = JSON.stringify(this.dataArray);
},
setCellText: function(idx,column,t) {
	if ("name"==column.id)	this.dataArray[idx][1] = escape(t);
	if ("url"==column.id)	this.dataArray[idx][2] = t;
	this.treeBox.invalidateRow(idx);
	this.dataElement.value = JSON.stringify(this.dataArray);
},
getCellText: function(idx, column) {
	if ("name"==column.id) return unescape(this.dataArray[idx][1]);
	if ("url"==column.id) return this.dataArray[idx][2];
	return "other text row " + idx;
},  
isContainer: function(idx)         { return false; },
isContainerOpen: function(idx)     { return false; },
isContainerEmpty: function(idx)    { return true; },
isSeparator: function(idx)         { return false; },
isSorted: function()               { return false; },
isEditable: function(idx, column)  { return true; },
getLevel: function(idx)		   {  return 0;  },  
toggleOpenState: function(idx) { },  

getImageSrc: function(idx, column) { return null; },
getProgressMode : function(idx,column) {},
cycleHeader: function(col, elem) {},
selectionChanged: function() {
	var idx = document.getElementById('seop-link-tree').currentIndex;
	if (idx<0 || idx>=links_view.dataArray.length) {
		document.getElementById('menulinks-del').enabled = false;
		return ;
	}
	document.getElementById('menulinks-del').disabled = this.dataArray[idx][3];
},
cycleCell: function(idx, column) {},
performAction: function(action) {},
performActionOnCell: function(action, index, column) {},
getRowProperties: function(idx, column, prop) {},
getCellProperties: function(idx, column, prop) {},
getColumnProperties: function(column, element, prop) {},

add_item: function() {
	links_view.dataArray.push([ true, "???", "http://localhost/", false ]);
	links_view.treeBox.rowCountChanged(links_view.dataArray.length-1, 1);  
},
del_selected: function() { 
	var idx = document.getElementById('seop-link-tree').currentIndex;
	if (links_view.dataArray[idx][3]) {
		return ;
	}
	links_view.dataArray.splice(idx, 1);
	links_view.treeBox.rowCountChanged(links_view.dataArray.length-1, -1);  
	links_view.dataElement.value = JSON.stringify(this.dataArray);
},
reset_links: function() {
	links_view.treeBox.rowCountChanged(0, -links_view.dataArray.length);
	links_view.dataArray = links_view.seop_global.menulinks_default;
	links_view.treeBox.rowCountChanged(0, links_view.dataArray.length);
	links_view.dataElement.value = JSON.stringify(this.dataArray);
},
__x_sentinel: 0
};

Components.utils.import("resource://seoprofesional/seopdb.jsm",		links_view);


window.addEventListener("load", function(e) { 
    links_view.init();
    document.getElementById('seop-link-tree').view = links_view;
}, false);

